package java_07;

import java.io.File;
import java.util.HashMap;

public class java_03 {
	public static void main(String[] args) {
		File File=new File("d:\\steam");
		HashMap<String, Long> fileTypeCount = new HashMap<>();
		countFiles(File, fileTypeCount);
		print(fileTypeCount);

	}

	public static void countFiles(File file, HashMap<String,Long> fileTypeCount) {
		if (file.exists() && file.isDirectory()) {
			File[] files = file.listFiles();
			if (files != null) { 
				for (File f : files) {
					if (f.isFile()) {
						String fileName = f.getName();
						String fileType = getFileExtension(fileName);
						fileTypeCount.put(fileType, fileTypeCount.getOrDefault(fileType, (long) 0) + 1);
					} else if (f.isDirectory()){
						countFiles(f, fileTypeCount);
					}
				}
			}
		}
	}
	public static String getFileExtension(String fileName) {
		int dotIndex = fileName.lastIndexOf('.');
		if (dotIndex !=-1) {
			return fileName.substring(dotIndex+1).toLowerCase();
		} else {
			return "";
		}
	}
	public static void print(HashMap<String, Long> fileTypeCount) {
		for (String fileType : fileTypeCount.keySet()) {
			System.out.println(fileType + ": " + fileTypeCount.get(fileType));
		}
	}
}
